<template>
	<view>
		<view class="position-flex-top pg-30 bgColor-white">
			<navigator url="/goodsPackage/address/address" class="title-button">+添加新的收货地址</navigator>
		</view>
		<view class="add-list px-30">
			<view class="item" @tap='choiceAdd(item)' v-for="(item,index) in data" :key='index' :class="index!=0?'border':''">
				<view class="d-flex aling-centen jfy-sb">
					<text>{{item.name}}</text>
					<view class="d-flex aling-centen">
						<view class="d-flex-yCenter" style="margin-right: 64rpx;" @tap.stop='updateAdd(item.id)'>
							<image src="../../static/icon/update.png" mode=""></image>
							<text>编辑</text>
						</view>
						<view class="d-flex-yCenter" @tap.stop='deleteAdd(item.id)'>
							<image src="../../static/icon/delete.png" mode=""></image>
							<text>删除</text>
						</view>
					</view>
				</view>
				<view class="mt-20">{{item.phone}}</view>
				<view class="py-20" style="width: 440rpx;">
					{{item | address}}
				</view>
				<view class="d-flex">
					<x-checkbox @change='change' :value="item.is_default" :name='item.id'>设为默认地址</x-checkbox>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		http_userAddress_lst,
		http_useraddress_set_default,
		http_useraddress_delete
	} from '@/config/apiRouter.js'
	export default {
		data() {
			return {
				actve: 'orange',
				page: 1,
				last_page: 1,
				data: [],
			}
		},
		filters: {
			address(e) {
				if (e.province != e.city) {
					return e.province + e.city + e.district + e.address + e.user_note
				}
				return e.province + e.district + e.address + e.user_note

			}
		},
		onReachBottom() {
			if (this.page == this.last_page || last_page == 0) return
			this.page++
			this.getData(true)
		},
		onShow() {
			this.page = 1
			this.getData()
		},
		methods: {
			choiceAdd(e){
				uni.$emit('update-address',e)
				this.$tool.navigateBack()
			},
			updateAdd(e){
				uni.navigateTo({
					url:'/goodsPackage/address/address?id='+e
				})
			},
			deleteAdd(e){
				uni.showModal({
				    title: '提示',
				    content: '确认删除收货地址吗？',
				    success:res=>{
				        if (!res.confirm) return
						http_useraddress_delete({
							data:{
								id:e
							}
						}).then(res=>{
							this.page = 1
							this.getData()
						})
				    }
				});
				
			},
			getData(statr) {
				http_userAddress_lst({
					page: this.page
				}).then(res => {
					res.data.data.map(item => {
						if (item.is_default) {
							item.checked = 'orange'
						} else {
							item.checked = ''
						}
					})
					this.last_page = res.data.last_page
					if (statr) {
						this.data = this.data.concat(res.data.data)
						return
					}
					this.data = res.data.data
				})
			},
			// 设为默认地址
			change(e) {
				if (e.type) return
				http_useraddress_set_default({
					data: {
						id: e.name
					},
				}).then(res => {
					uni.showToast({
						title: '修改成功'
					})
					setTimeout(() => {
						this.page = 1
						this.getData()
					})
				})
			}
		}
	}
</script>

<style lang="scss">
	.title-button {
		line-height: 70rpx;
		text-align: center;
		background: #FFFFFF;
		border: 1px solid #000000;
		border-radius: 12rpx;
	}

	.add-list {
		margin-top: 160rpx;
		padding-bottom: 30rpx;

		.item {
			image {
				width: 28rpx;
				height: 32rpx;
				margin-right: 10rpx;
			}
		}

		.border {
			border-top: 1px solid #ddd;
			padding-top: 30rpx;
			margin-top: 30rpx;
		}
		
	}
</style>
